import { useLocalStorage } from '@peng_kai/kit/libs/vueuse';

const invitedSinpStorage = useLocalStorage('invitedSpin', {});

export const tipsState = ref(true);

// 请求概率数组
export function useList() {
  const { data, isLoading, refetch, isSuccess } = useQuery({
    queryKey: [apis.apiActivityInviteReady.id],
    queryFn: () => apis.apiActivityInviteReady({ is_luckyspin: ~~invitedSinpStorage.value.firstDraw }),
  });

  // 设置默认值，让canvas马上绘制
  const list = computed(() => data?.value?.circle ?? []);
  const Loading = computed(() => isLoading?.value || false);
  const status = computed(() => data?.value?.status ?? undefined);
  const count = computed(() => data.value?.status?.count ?? 0);

  const difference = computed(() => Number((status.value?.withdraw_amount ?? 0)) - Number((status.value?.amount ?? 0)));
  const amount = computed(() => Number(status.value?.amount ?? 0) ?? 0);
  const orderOn = computed(() => status.value?.order_no ?? '');
  const currency = computed(() => status.value?.currency ?? '-');
  const withdrawAmount = computed(() => status.value?.withdraw_amount ?? '0');
  const symbol = computed(() => ({ BRL: 'R$', PKR: 'Rs' }));
  const isRs = computed(() => (currency.value === 'PKR'));

  return {
    list,
    Loading,
    isSuccess,
    status,
    refetch,
    difference,
    amount,
    orderOn,
    count,
    currency,
    symbol,
    withdrawAmount,
    isRs,
  };
}

// 请求历史数据
export function useHistoryList() {
  const logQuery = useQuery({
    queryKey: [apis.apiActivityInviteHistory.id],
    queryFn: async () => await apis.apiActivityInviteHistory(undefined),

  });

  const list = computed(() => logQuery.data.value ?? []);

  return {
    list,
    logQuery,
  };
}

// 处理分享
export async function handleShare(status: number | undefined, receive?: boolean) {
  const authed = useAuthed();
  if (authed.value) {
    if (status === 2 && !receive) {
      await apis.apiActivityInviteWithdraw(undefined);
      antdMessage.info($t('9jI2m3ZiANn0KfHwcesRv'));
      globalPopups.invitationSpinPayouts?.open();
    }
    else {
      globalPopups.invitedShare?.open();
    }
  }
  else {
    globalPopups.sign.open?.({ view: 'signUp' });
  }
}
